/* $RCSFile: ADNCRM.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/ADNCRM.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:27  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Web;
using System.Diagnostics;
using System.ComponentModel;
using System.Globalization;

using ADNCRM.ServerControls;
using AddonNice.Configuration;
using AddonNice.CRMWrapper;
using AddonNice.Security;
using AddonNice.Diagnostics;

namespace AddonNice.Core
{
    /// <summary>
    /// Class used for license testing
/// </summary>
    [
    LicenseProvider(typeof(ADNEncryptedLicenseProvider))
    ]
    internal sealed class AddonNice
    {

        private ADNEncryptedLicense _Lic = null;
        public AddonNice()
        {
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice Ctor BEG at {0}.", DateTime.Now),LicenseTraceSwitch.Sw.Info);
            if (!ADNBaseLicense.IsExternalLicenseCountEventOk())
            {
                if ( LicenseTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice Ctor Adding ExternalLicenseCountHandler at {0}.", DateTime.Now));
                ADNBaseLicense.LicenseCountEvent += new ADNCRM.ServerControls.ExternalLicenseCountHandler(AddonNice.CheckCRMUsersCount);
            }
            _Lic = (ADNEncryptedLicense)LicenseManager.Validate(typeof(AddonNice), this);
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice Ctor END Info: {0}.",License.Info));
        }

        public bool DemoMode
        {
            get
            {
                bool IsDemo =   License.IsDemo;
                if ( LicenseTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice DemoMode GET: {0}, Info: {1}.",IsDemo,License.Info));
                return IsDemo;
            }
        }
        
        public ADNEncryptedLicense License
        {
            get
            {            return _Lic;}
        }

        /// <summary>
        /// Cherck that crm systemUsers are less or equal to the license number bought
        /// Must be called after full initialization is Ok: PortalSettings must be created and stored in session
        /// </summary>
        /// <param name="lic"></param>
        /// <returns></returns>
        public static int CheckCRMUsersCount(ADNBaseLicense lic)
        {
            int ret = -1; // no users so license 
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice CheckCRMUsersCount enter at {0}.", DateTime.Now),LicenseTraceSwitch.Sw.Info);
            if (HttpContext.Current != null && (HttpContext.Current.Items[StStr.strPortalSettings] != null))
            {
                CRMWrapperBase Wrapper = PortalSecurity.Wrapper;
                ret= Wrapper.ActiveUsersCount;
            }
            if ( LicenseTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddonNice CheckCRMUsersCount : nb={0} at {1}.", ret, DateTime.Now),LicenseTraceSwitch.Sw.Info);
            return ret;
        }
    }

}
